package com.cuitrip.business.tripservice;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cuitrip.business.tripservice.event.ServiceChangeEvent;
import com.cuitrip.business.tripservice.model.FormType;
import com.cuitrip.business.tripservice.model.ServiceInfo;
import com.cuitrip.component.item.ItemLayout;
import com.cuitrip.service.R;
import com.cuitrip.util.ResourceUtil;
import com.lab.app.customui.CustomUiConfig;
import com.lab.app.customui.CustomUiFragment;
import com.lab.jumper.FragmentJumper;
import com.lab.rx.bus.RxBus;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by sheena on 16/4/19.
 */
public class CreateTripDescFragment extends CustomUiFragment {

    @Bind(R.id.il_trip_story)
    ItemLayout mTripStroyLayout;

    @Bind(R.id.il_trip_detail)
    ItemLayout mTripDetailLayout;

    @Bind(R.id.il_trip_highlights)
    ItemLayout mTripHighlightLayout;

    private ServiceInfo mServiceInfo;

    public static CreateTripDescFragment newInstance(ServiceInfo serviceInfo){
        CreateTripDescFragment createTripDescFragment = new CreateTripDescFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(ServiceInfo.KEY,serviceInfo);
        createTripDescFragment.setArguments(bundle);
        return createTripDescFragment;
    }

    @Override
    public boolean initCloseTopBar() {
        return false;
    }

    @Override
    public CustomUiConfig initCustomUiConfig() {
        CustomUiConfig config = new CustomUiConfig();
        config.mLeftStyle= CustomUiConfig.AreaStyle.ICON_STYLE;
        config.mTopbarLeftAreaName=getString(R.string.back_icon);
        config.mTopbarMiddleAreaName=getString(R.string.trip_content_detail);
        return config;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState, R.layout.fragment_create_trip_desc);
        if (getArguments()!=null){
            mServiceInfo = (ServiceInfo) getArguments().getSerializable(ServiceInfo.KEY);
        }
        return view;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this,getView());
        if (mServiceInfo != null) {
            renderDescriptionStory();

            renderServiceDescript();

            renderHighLight();
        }
    }

    private void renderHighLight() {
        if (!TextUtils.isEmpty(mServiceInfo.getHighLight())) {
            mTripHighlightLayout.setLeftIconColor(ResourceUtil.getColor(R.color.apple_7ecc1e));
        } else {
            mTripHighlightLayout.setLeftIconColor(ResourceUtil.getColor(R.color.ganshi_ded8d7));
        }
    }

    private void renderServiceDescript() {
        if (!TextUtils.isEmpty(mServiceInfo.getServiceDescript())) {
            mTripDetailLayout.setLeftIconColor(ResourceUtil.getColor(R.color.apple_7ecc1e));
        } else {
            mTripDetailLayout.setLeftIconColor(ResourceUtil.getColor(R.color.ganshi_ded8d7));
        }
    }

    private void renderDescriptionStory() {
        if (!TextUtils.isEmpty(mServiceInfo.getServiceStory())) {
            mTripStroyLayout.setLeftIconColor(ResourceUtil.getColor(R.color.apple_7ecc1e));
        } else {
            mTripStroyLayout.setLeftIconColor(ResourceUtil.getColor(R.color.ganshi_ded8d7));
        }
    }

    @OnClick(R.id.il_trip_story)
    public void completeStory(){
        CreateTripDescStoryFragment createTripDescStoryFragment = CreateTripDescStoryFragment.newInstance(mServiceInfo);
        createTripDescStoryFragment.setResultCallBack(new OnResultCallback() {
            @Override
            public void onResultBack(ServiceInfo serviceInfo) {
                renderDescriptionStory();
            }
        });
        FragmentJumper.addFragment(getHostActivity().getCustomSelfFragmentManager(), R.id.container, createTripDescStoryFragment, CreateTripDescStoryFragment.class.getName(), true);
    }

    @OnClick(R.id.il_trip_detail)
    public void completeTripDetail(){
        CreateTripDetailFragment createTripDetailFragment = CreateTripDetailFragment.newInstance(mServiceInfo);
        createTripDetailFragment.setResultCallBack(new OnResultCallback() {
            @Override
            public void onResultBack(ServiceInfo serviceInfo) {
                renderServiceDescript();

            }
        });
        FragmentJumper.addFragment(getHostActivity().getCustomSelfFragmentManager(),R.id.container,createTripDetailFragment,CreateTripDetailFragment.class.getName(),true);
    }

    @OnClick(R.id.il_trip_highlights)
    public void completeHighlight(){
        CreateTripDescHighlightsFragment createTripDescHighlightsFragment = CreateTripDescHighlightsFragment.newInstance(mServiceInfo);
        createTripDescHighlightsFragment.setResultCallBack(new OnResultCallback() {
            @Override
            public void onResultBack(ServiceInfo serviceInfo) {
                renderHighLight();
            }
        });
        FragmentJumper.addFragment(getHostActivity().getCustomSelfFragmentManager(),R.id.container,createTripDescHighlightsFragment,CreateTripDescHighlightsFragment.class.getName(),true);
    }

    private void saveAction() {
        RxBus.getDefault().send(new ServiceChangeEvent(FormType.DESCRIPTION));
    }

    @Override
    public void onTopbarLeftPress() {
        onBackPressed(false);
    }

    @Override
    public boolean onBackPressed(boolean fromKey) {
        saveAction();
        return super.onBackPressed(fromKey);
    }


    public interface OnResultCallback {
        public void onResultBack(ServiceInfo serviceInfo);
    }
}
